<script>
	export default {
		onLaunch: function() {
			console.log('App Launch')
			
			const appKey = 'ding4xyxwffxxjgnhdr2';
			// 获取code
			dd.getAuthCode({
			    success:function(res){
					console.log('authCode'+res.authCode);
					// 获取
					dd.httpRequest({
						url: 'http://113.15.231.21:180/dd/ding_oauth',
						method: 'POST',
						data: {
							app_key:appKey,
							code:res.authCode
						},
						dataType: 'json',
						success: function(res1) {
							dd.showToast({
								content: "登录成功",
								type: 'success',
							})
							console.log(res1);
							
							dd.setStorageSync({
							  key: 'storageData',
							  data: {
							   ding_userid: res1.data.result.ding_userid,
							   sessionid: res1.data.result.sessionid,
							   user_id: res1.data.result.user_id
							  }
							});
							
						},
						fail: function(res1) {
							dd.showToast({
								content: "请求ding_oauth接口失败 "+res1,
								type: 'fail',
							})
						}
					});
					
			    },
			    fail:function(err){
					dd.showToast({
						content: "获取authCode失败 "+err,
						type: 'fail',
					})
			    }
			});
			
			//如果不能从缓存中获取sessionid，将重新登录
			if(!this.$root?.sessionID){
				const appKey = 'ding4xyxwffxxjgnhdr2';
				// 获取code
				dd.getAuthCode({
				    success:function(res){
						console.log('authCode'+res.authCode);
						// 获取
						dd.httpRequest({
							url: 'http://113.15.231.21:180/dd/ding_oauth',
							method: 'POST',
							data: {
								app_key:appKey,
								code:res.authCode
							},
							dataType: 'json',
							success: function(res1) {
								dd.showToast({
									content: "登录成功",
									type: 'success',
								})
								console.log(res1);
								
								dd.setStorageSync({
								  key: 'storageData',
								  data: {
								   ding_userid: res1.data.result.ding_userid,
								   sessionid: res1.data.result.sessionid,
								   user_id: res1.data.result.user_id
								  }
								});
								
							},
							fail: function(res1) {
								dd.showToast({
									content: "请求ding_oauth接口失败 "+res1,
									type: 'fail',
								})
							}
						});
				    },
					fail:function(err){
						dd.showToast({
							content: "获取authCode失败 "+err,
							type: 'fail',
						})
					}
				});
			}

		},
		onShow: function() {
			console.log('App Show')
			const updateManager = dd.getUpdateManager()
			updateManager.onCheckForUpdate(function (res) {
			  // 请求完新版本信息的回调
			  console.log(res.hasUpdate) // 是否有更新
			})
			
			
			updateManager.onUpdateReady(function (ret) {
			  console.log(ret.version) // 更新版本号
			  dd.confirm({
			    title: '更新提示',
			    content: '新版本已经准备好，是否重启应用？',
			    success: function (res) {
			      if (res.confirm) {
			        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
			        updateManager.applyUpdate()
			      }
			    }
			  })
			})
		},
		onHide: function() {
			console.log('App Hide')
		},
	}
</script>

<style lang="scss">

	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	@import "@/static/fonts/iconfont.css";
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
</style>
